package process

import (
	"fmt"
	"matching/engine"
	"matching/enum"
	"matching/errcode"
	"matching/jobline"
	"matching/middleware/cache"
	"matching/model"
	"time"
)


// 分发订单
func Dispatch(order model.Order) errcode.ErrorCode {
	if engine.ChanMap[order.Symbol] == nil {
		return errcode.EngineNotFound
	}

	if order.Action == enum.ActionCreate {
		if cache.OrderExist(order.Symbol,order.OrderId) {
			return errcode.OrderExist
		}
	} else {
		if !cache.OrderExist(order.Symbol,order.OrderId) {
			return errcode.OrderNotFound
		}
	}

	order.Timestamp = time.Now().UnixNano() / 1e3
	cache.SaveOrderObject(order)
	cache.SaveOrderSort(order)
	engine.ChanMap[order.Symbol] <- order
	return errcode.Success
}



func DispatchTest(order model.Order) errcode.ErrorCode {
	order.Timestamp = time.Now().UnixNano() / 1e3
	engine.ChanMap[order.Symbol] <- order
	if jobline.ChanSaveOrderMap[order.Symbol]==nil{
		fmt.Printf("chan symbol %s is nill \n",order.Symbol)
	}
	if order.OrderId=="OXT008"{
		fmt.Print()
	}
	jobline.ChanSaveOrderMap[order.Symbol] <- order
	cache.SaveOrderSort(order)
	return errcode.Success
}